import axios from 'axios'
import type { Product, Order, User, Coupon, Promotion } from '@/types'

const api = axios.create({
  baseURL: 'http://localhost:5000/api',
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 响应拦截器
api.interceptors.response.use(
  response => response.data,
  error => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
    }
    return Promise.reject(error.response?.data || error)
  }
)

// 商品相关接口
export const productApi = {
  getList: (params?: any) => api.get('/products', { params }),
  getById: (id: string) => api.get(`/products/${id}`),
  create: (data: Partial<Product>) => api.post('/products', data),
  update: (id: string, data: Partial<Product>) => api.put(`/products/${id}`, data),
  delete: (id: string) => api.delete(`/products/${id}`),
  updateStatus: (id: string, status: string) => api.patch(`/products/${id}/status`, { status })
}

// 订单相关接口
export const orderApi = {
  getList: (params?: any) => api.get('/orders', { params }),
  getById: (id: string) => api.get(`/orders/${id}`),
  update: (id: string, data: Partial<Order>) => api.put(`/orders/${id}`, data),
  updateStatus: (id: string, status: string) => api.patch(`/orders/${id}/status`, { status }),
  delete: (id: string) => api.delete(`/orders/${id}`)
}

// 库存相关接口
export const inventoryApi = {
  getList: (params?: any) => api.get('/inventory', { params }),
  update: (productId: string, quantity: number) => api.put(`/inventory/${productId}`, { quantity }),
  getHistory: (productId: string) => api.get(`/inventory/${productId}/history`)
}

// 配送相关接口
export const deliveryApi = {
  getList: (params?: any) => api.get('/delivery', { params }),
  create: (data: any) => api.post('/delivery', data),
  update: (id: string, data: any) => api.put(`/delivery/${id}`, data),
  updateStatus: (id: string, status: string) => api.patch(`/delivery/${id}/status`, { status })
}

// 用户相关接口
export const userApi = {
  getList: (params?: any) => api.get('/users', { params }),
  getById: (id: string) => api.get(`/users/${id}`),
  create: (data: Partial<User>) => api.post('/users', data),
  update: (id: string, data: Partial<User>) => api.put(`/users/${id}`, data),
  delete: (id: string) => api.delete(`/users/${id}`),
  updateStatus: (id: string, status: string) => api.patch(`/users/${id}/status`, { status })
}

// 优惠券相关接口
export const couponApi = {
  getList: (params?: any) => api.get('/coupons', { params }),
  getById: (id: string) => api.get(`/coupons/${id}`),
  create: (data: Partial<Coupon>) => api.post('/coupons', data),
  update: (id: string, data: Partial<Coupon>) => api.put(`/coupons/${id}`, data),
  delete: (id: string) => api.delete(`/coupons/${id}`),
  claim: (id: string) => api.post(`/coupons/${id}/claim`)
}

// 促销活动相关接口
export const promotionApi = {
  getList: (params?: any) => api.get('/promotions', { params }),
  getById: (id: string) => api.get(`/promotions/${id}`),
  create: (data: Partial<Promotion>) => api.post('/promotions', data),
  update: (id: string, data: Partial<Promotion>) => api.put(`/promotions/${id}`, data),
  delete: (id: string) => api.delete(`/promotions/${id}`),
  updateStatus: (id: string, status: string) => api.patch(`/promotions/${id}/status`, { status })
}

// 统计相关接口
export const statsApi = {
  getDashboard: () => api.get('/stats/dashboard'),
  getSales: (period: string) => api.get(`/stats/sales?period=${period}`),
  getHotProducts: () => api.get('/stats/hot-products'),
  getRecentOrders: () => api.get('/stats/recent-orders')
}

export default api 